package com.stefanini.worldcupinformation.controladores.team;

import com.stefanini.worldcupinformation.ejbs.GroupDao;
import com.stefanini.worldcupinformation.ejbs.TeamDao;
import com.stefanini.worldcupinformationpersistence.entities.Equipo;
import com.stefanini.worldcupinformationpersistence.entities.Grupo;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.context.FacesContext;
import javax.inject.Named;

@Named("newTeamController")
@SessionScoped
public class NewTeamController implements Serializable{
    private static final long serialVersionUID = 1L;
    
    @EJB
    private GroupDao groupDao;
    
    @EJB
    private TeamDao teamDao;
    
    private Equipo team;
    private List<Grupo> groups;

    public List<Grupo> getGroups() {
        return groups;
    }
    
    public void init(){
        this.team = new Equipo();
        this.team.setGrupoId(new Grupo());
        this.initGroups();
    }
    
    public void save(){
        try {
            this.teamDao.newTeam(this.team);
            FacesContext.getCurrentInstance().getExternalContext()
                    .redirect("/worldCupInformation/pages/team/teamAdmin.xhtml");
        } catch (IOException ex) {
            Logger.getLogger(NewTeamController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public Equipo getTeam() {
        return team;
    }

    public void setTeam(Equipo team) {
        this.team = team;
    }
    
    private void initGroups(){
        this.groups = this.groupDao.getAll();
    }
}
